2 Problem: 11504 - Dominos
3 Author: Andrés Mejía-Posada
4 (http://blogaritmo.factorcomun.org)
18 enum { standing
, tiled
, handed
};
20 void dfs(int u
, int root
){
21 if (color
[u
] != standing
) return;
23 if (u
== root
) color
[u
] = handed
;
24 else color
[u
] = tiled
;
26 vector
<int> &out
= g
[u
];
27 for (int k
=0, m
=out
.size(); k
<m
; ++k
){
29 if (color
[v
] == handed
&& v
!= root
) color
[v
] = tiled
;
30 else if (color
[v
] == standing
) dfs(v
, root
);
37 int n
, m
; cin
>> n
>> m
;
39 assert(n
< N
&& m
< N
);
41 for (int i
=0; i
<=n
; ++i
) g
[i
].clear(), color
[i
] = standing
;
42 for (int u
, v
, i
=0; i
<m
&& cin
>> u
>> v
; ++i
) g
[u
].push_back(v
);
43 for (int i
=1; i
<=n
; ++i
) if (color
[i
] == standing
) /*printf("Calling dfs(%d)...\n", i),*/ dfs(i
, i
);
46 for (int i
=1; i
<=n
; ++i
){
47 //if (color[i] == handed) printf("Knock down tile %d\n", i);
48 ans
+= (color
[i
] == handed
);